package org.ChrisRiffleSoftware.WhatIfJesusIsGod.ChuckNorrisJoker;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;

/**
 * ChuckNorrisJokerActivity
 * @author RIF
 * 
 */
public class ChuckNorrisJokerActivity extends Activity implements View.OnClickListener, OnInitListener{

	
	//*************************************************************
	//*************************************************************
	//				members
	//*************************************************************
	//*************************************************************
	
	private ChuckViewController chuckViewController;
	
	private List<String> jokeList1;
	private List<String> jokeList2;
	
	private boolean bUseList1 = true;
	
	private Random randomGenerator = new Random();
	
	private TextToSpeech txtToSpeech;

	private boolean txtToSpeechHasEnglish = false;
	
	
	//*************************************************************
	//*************************************************************
	//				overrides
	//*************************************************************
	//*************************************************************
	
	
	
	 // Called when the activity is first created.
	@Override
	public void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
	
		txtToSpeech = new TextToSpeech(this,this);
		
		setContentView(getChuckViewController().getViewLayout());
		
		//default sound on
		getChuckViewController().getSoundOnCheckBox().setChecked(true);
	}



	//*************************************************************
	//*************************************************************
	//				methods
	//*************************************************************
	//*************************************************************
	
	
	
	@Override
	public void onClick(View arg0) {
		
		String joke = getJoke();
		
		getChuckViewController().getJokeTxtView().setText(joke);
		
		if(txtToSpeechHasEnglish && getChuckViewController().getSoundOnCheckBox().isChecked()){
			txtToSpeech.speak(joke, TextToSpeech.QUEUE_FLUSH, null);
		}
		
	}



	// for text to speech
	@Override
	public void onInit(int status) {
		 
		getChuckViewController().getSubmitButton().setText("Loading...");
		getChuckViewController().getSubmitButton().setEnabled(false);
		
		 if (status == TextToSpeech.SUCCESS) {
			 
			 int result = txtToSpeech.setLanguage(Locale.US);
			 
			 if(result == TextToSpeech.LANG_MISSING_DATA 
			   || result == TextToSpeech.LANG_NOT_SUPPORTED){
				 
			 
				 txtToSpeechHasEnglish = false;
			 }else{
				 
				 txtToSpeechHasEnglish = true;
			 }
		 }
		
		 //done initializing, so we can enable the button now
		 getChuckViewController().getSubmitButton().setText("DANGER");
		getChuckViewController().getSubmitButton().setEnabled(true);
	}



	/**
	 * makes an internal copy of the orig list w/items loaded from the 
	 * array w/array xml file so the new copyArrayList will be mutable
	 * @return
	 */
	private List<String> getJokeList1(){
		if(jokeList1 == null){			
			String[] jokeArrayFromDisk = getResources().getStringArray(R.array.jokes);	
			
			List<String> immutableList = Arrays.asList(jokeArrayFromDisk); 
			
			//a mutable one(important so we can later remove jokes once they've been 
			// displayed so the user won't get bored seeing the same dumb jokes 
			// repeated all the time
			jokeList1 = new ArrayList<String>(immutableList);
		}
		return jokeList1;
	}
	


	private List<String> getJokeList2(){
		if(jokeList2 == null){
			jokeList2 = new ArrayList<String>();
		}
		return jokeList2;
	}
	
		
	
	/**
	 * 
	 * @param pJokeList
	 * @return null if list is out of jokes.  Returns the removed 
	 * random joke if the lists had some jokes left
	 */
	private String getJoke(){				

		if(bUseList1 && getJokeList1().isEmpty()){
			//recurse to list 2(b/c list 1 is empty)
			bUseList1 = false;
			getJoke();
									
		}else if(!bUseList1 && getJokeList2().isEmpty()){
			//back to list1(b/c list2 is empty)
			bUseList1 = true;
			getJoke();
		}				
			
		String joke = null;
		
		if(bUseList1){			
			int randomIndex = randomGenerator.nextInt(getJokeList1().size());			
			joke = getJokeList1().remove(randomIndex);
			getJokeList2().add(joke);
		}else{			
			int randomIndex = randomGenerator.nextInt(getJokeList2().size());
			joke = getJokeList2().remove(randomIndex);
			getJokeList1().add(joke);
		}								
		return joke;
	}
	

	/**
	 * getChuckViewController
	 * @return
	 */
	private ChuckViewController getChuckViewController() {
		if (chuckViewController == null) {
			chuckViewController = new ChuckViewController(this);
		}
		return chuckViewController;
	}
	

}



